package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.utils.page.PageBean;
import com.wf.course.model.ad.Keyword;
import com.wf.course.service.IKeywordService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 热搜词Controller
 */
@RequestMapping("keyword")
@Controller
public class KeywordController {

	@Autowired
	private IKeywordService kwService;

	/**
	 * 热搜词列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param keyword
	 * @param userName
	 * @param model
	 * @return
	 */
	@LogInfo("查询热搜词列表信息")
	@RequestMapping("keywordList")
	public String keyword(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "keyword", required = false) String keyword, 
			@RequestParam(value = "userName", required = false) String userName, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("keyword", keyword);
		cons.put("userName", userName);
		PageBean<Keyword> pageBean = new PageBean<Keyword>(pageNo, pageSize, null, cons);
		pageBean.setUrl("keyword/keywordList");
		this.kwService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "course/keywordList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改热搜词，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			Keyword Keyword = kwService.queryKeywordById(id);
			model.addAttribute("Keyword", Keyword);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 
		
		return "course/keywordEdit";
	}

	/**
	 * 新增和修改
	 * 
	 * @param Keyword
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改热搜词")
	@RequestMapping(value = "keywordEdit" , produces = "text/html;charset=UTF-8")
	public String KeywordEdit(@ModelAttribute Keyword keyword, HttpServletRequest request) {
		
		kwService.saveOrUpdateKeyword(keyword);
		
		return "redirect:/keyword/keywordList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除热搜词")
	@RequestMapping(value = "remove/{id}")
	public String removeKeyword(@PathVariable("id") Integer id) {
		
		kwService.deleteById(id);
		
		return "redirect:/keyword/keywordList";
	}
	

}
